/**Carga imagenes y dispara una funcion callback al finalizar.
 *  */
function ImgLoader(toload , completeCallback) {
    this.imagesToLoad = toload;
    this.numberOfImagesLoaded = 0;
    this.loadedImages = {};
    this.completeCallback = completeCallback;
}


ImgLoader.prototype.handleImageLoad = function(img,imgId) {
        this.numberOfImagesLoaded++;

        console.log("successfully loaded "+imgId+" : "+img.src);        
        
        var id = (imgId) ? imgId : "img_"+this.numberOfImagesLoaded;
        this.loadedImages[id] = img;
        
        
        // We're not starting the game until all images are loaded
        // Otherwise, you may start to draw without the resource and raise
        // this DOM Exception: INVALID_STATE_ERR (11) on the drawImage method
        if (this.numberOfImagesLoaded == this.imagesToLoad) {
            this.numberOfImagesLoaded = 0;
            this.completeCallback();
        }
};

ImgLoader.prototype.handleImageError = function(e) {
     console.log("Error Loading Image : " + e.target.src);
};

ImgLoader.prototype.load = function(imgSrc , imgId) {
    var img = new Image();
    img.onload = this.handleImageLoad.bind(this , img, imgId);
    img.onerror = this.handleImageError.bind(this);
    img.src = imgSrc;
    
    return img;
};